package com.zl.learning.lambada;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SpliteratorTest {

    @Test
    public void test1() {
        long start = System.currentTimeMillis();
        List<Integer> list = Stream.iterate(0, a -> a + 2).limit(100).collect(Collectors.toList());
        System.out.println(list.size());
        Spliterator<Integer> s1 = list.spliterator();
        System.out.println(s1.estimateSize());
        Queue<Spliterator<Integer>> queue = new LinkedList<>();
        queue.offer(s1);
        List<Spliterator<Integer>> res = new ArrayList<>();
        while (queue.size() > 0) {
            Spliterator<Integer> s = queue.poll();
            while (s.estimateSize() > 10) {
                Spliterator<Integer> s2 = s.trySplit();
                queue.offer(s2);
            }
            res.add(s);
        }
//        System.out.println(res.size());
//        res.forEach(r -> System.out.println(r.estimateSize()));
//        res.stream().parallel().forEach(r -> r.forEachRemaining(this::print));
        long end = System.currentTimeMillis();
        System.out.println("const:" + (end - start));
        //const:1372
    }

    @Test
    public void test2() {
        long start = System.currentTimeMillis();
        List<Integer> list = Stream.iterate(0, a -> a + 2).limit(100).collect(Collectors.toList());
        System.out.println(list.size());
        list.forEach(this::print);
        long end = System.currentTimeMillis();
        System.out.println("const:" + (end - start));
        //const:1372
        //const:10108
    }

    private void print(Integer i) {
        System.out.println(i);
//        try {
//            Thread.sleep(100);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
